﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using WebMatrix.WebData;

using iKnow.Models;
using iKnow.Extensions;

namespace iKnow.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/

        public JsonResult Article(FormCollection form)
        {
            iKnowEntities entities
                = new iKnowEntities();

            var articles = from a in entities.Article.AsEnumerable()
                           group a by a.Title into g
                           select g.WithMax(r => r.Version);


            var titles
                = (from a in articles.AsEnumerable()
                   where a.Title.ToLower().Contains(form["search"].ToLower())
                   || a.Text.ToLower().Contains(form["search"].ToLower())
                   select a.Title).ToArray<String>();

            return Json(titles, JsonRequestBehavior.AllowGet);
        }

    }
}
